//
//  TTScrollableAds.m
//  TaizhouTong
//
//  Created by Frank on 3/4/14.
//  Copyright (c) 2014 whale.fin. All rights reserved.
//

#import "TTScrollableAds.h"
#import "supportingHeadFiles.h"

@interface TTScrollableAds() <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) UIPageControl *myPageControl;
@property (nonatomic) BOOL changingPageControl;

@end

@implementation TTScrollableAds

- (UIPageControl *)myPageControl
{
    if (!_myPageControl) {
        _myPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.bounds.size.width/2 - 19, self.bounds.size.height - 40, 38, 36)];
        [self addSubview:_myPageControl];
    }
    return _myPageControl;
}

- (void)setImageArray:(NSArray *)imageArray
{
    if (_imageArray != imageArray) {
        _imageArray = imageArray;
        [self setupScrollView];
    }
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        //Initialization code
    }
    return self;
}

- (void)setupScrollView
{
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    if (self.imageArray.count != 0) {
        self.myScrollView.contentSize = CGSizeMake(self.bounds.size.width * self.imageArray.count,self.bounds.size.height);
    }
    [self.myScrollView setBackgroundColor:[UIColor blackColor]];                    //设置背景颜色
    self.myScrollView.pagingEnabled = YES;                                         //分页时候自动跳转到边界
    self.myScrollView.showsHorizontalScrollIndicator = NO;                         //控制水平方向是否滚动
    self.myScrollView.showsVerticalScrollIndicator = NO;                           //控制垂直方向是否滚动
    self.myScrollView.directionalLockEnabled = YES;                                //是否锁定跟踪滚动方向
    self.myScrollView.alwaysBounceVertical = NO;                                   //是否垂直回弹
    self.myScrollView.bounces = NO;                                                //是否有回弹效果
    self.myScrollView.scrollsToTop = NO;                                           //是否滚动到TOP
    self.myScrollView.clipsToBounds = YES;                                         //修剪边界
    self.myScrollView.scrollEnabled = YES;
    self.myScrollView.delegate = self;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
    [self.myScrollView addGestureRecognizer:tapGesture];
    
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonSet)];
    recognizer.numberOfTouchesRequired = 1;
    recognizer.numberOfTapsRequired = 1;
    [self.myScrollView addGestureRecognizer:recognizer];

    
    [self addSubview:self.myScrollView];
    [self loadScrollViewContent];
}

- (void)loadScrollViewContent
{
//    self.myPageControl.currentPage = self.index;
    CGFloat imageOriginX = 0;
    
    for (NSURL *imageUrl in self.imageArray) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"defaultImage"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        [self.myScrollView addSubview:imageView];
        imageView.frame = CGRectMake(imageOriginX, 0, self.bounds.size.width, self.bounds.size.height);
        imageOriginX += self.bounds.size.width;
    }
//    self.myPageControl.numberOfPages = self.imageArray.count;
    
    if (self.imageArray.count == 0) {
        self.myScrollView.contentSize = CGSizeMake(320, 1);
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultImage"]];
        imageView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        [self.myScrollView addSubview:imageView];
    }
    [self.myScrollView scrollRectToVisible:CGRectMake(320*self.index,0,320,210) animated:NO];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"navigationHide" object:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.myScrollView]) {
//        if (self.changingPageControl) {
//            return;
//        }
//        [self.myScrollView setContentOffset:CGPointMake(0, -self.myScrollView.contentOffset.y)];
        CGFloat pageWidth = self.myScrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1;
        self.myPageControl.currentPage = page;
    }
}

- (void)scrollViewTapped:(UITapGestureRecognizer *)sender
{
    if (self.imageArray.count != 0) {
        [self.delegate adClickedAtIndex:[self.myPageControl currentPage]];
    }
}

-(void)buttonSet
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"viewCallBack" object:nil];
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    if ([scrollView isEqual:self.myScrollView]) {
//        self.changingPageControl = NO;
//    }
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
